package src.math;

public class Integ
{
	public static double	GM	= 0;

	public static Var		V	= null;
	public static Var		F1	= null;
	public static Var		F2	= null;
	public static Var		F3	= null;

	public static double	t;
	public static double	st;

	public static void init(double x, double y, double vx, double vy, double t, double st, double GM)
	{
		Integ.V = new Var();
		Integ.F1 = new Var();
		Integ.F2 = new Var();
		Integ.F3 = new Var();

		Integ.V.x = x;
		Integ.V.y = y;
		Integ.V.vx = vx;
		Integ.V.vy = vy;

		Integ.t = t;
		Integ.st = st;
		Integ.GM = GM;
	}

	static Var stepRungeKutta4()
	{
		double st2 = st / 2.0;
		Var K1 = Kepler.RHS(V, GM, t);
		//V + st2 * K1
		Var K2 = Kepler.RHS(Var.add(V, Var.mult(st2, K1)), GM, t + st2);
		//V + st2 * K2
		Var K3 = Kepler.RHS(Var.add(V, Var.mult(st2, K2)), GM, t + st2);
		//V + st * K3
		Var K4 = Kepler.RHS(Var.add(V, Var.mult(st, K3)), GM, t + st);
		//V = V + (st / 6.) * (K1 + 2. * K2 + 2. * K3 + K4);
		V = Var.add(V, Var.mult(st / 6.0, Var.add(Var.add(K1, Var.mult(2.0, K2)), Var.add(Var.mult(2.0, K3), K4))));

		t += st;
		return K1;
	}

	public static void startForAdams4()
	{
		F3 = stepRungeKutta4();
		F2 = stepRungeKutta4();
		F1 = stepRungeKutta4();
	}

	public static void adamsBashforth4step()
	{
		Var F = Kepler.RHS(V, GM, t);
		//V = V + (st / 24.) * ( 55. * F + (- 59.) * F1	+ 37. * F2 + (- 9.) * F3 );
		V = Var.add(V, Var.mult(st / 24.0, (Var.add(Var.add(Var.mult(55.0, F), Var.mult(-59.0, F1)), Var.add(Var.mult(37.0, F2), Var.mult(-9.0, F3))))));
		t += st;
		F3 = F2;
		F2 = F1;
		F1 = F;
	}
}
